<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" type="text/css" href="css/ordermanager.css"/>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/jquery.cookie.js"></script>
</head>
<body>
    <div class="container" id="returnindex">
        <div class="row">
            <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="returnshop"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="ordermanage"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"><a href="index.html">返回首页</a></div>
        </div>
    </div>
    <div class="container" id="pagetitle">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3"><p id="titlep"><span>乐享生活</span>&nbsp;订单管理</p></div>
            <div class="col-xs-5 col-sm-5 col-md-5 col-lg-5"></div>
            <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2">
                <div class="row" id="shopinformation">
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12" id="loadshop"></div>
                </div>
            </div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
        </div>
    </div>

    <div class="container" id="managebody">
        <div class="row">
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1"></div>
            <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="manageleft">
                <div class="choise" id="wait">待处理</div>
                <div class="unchoise" id="already">已处理</div>
            </div>
            <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9" id="manageright"></div>
        </div>
    </div>

    <script type="text/javascript">

        var shopid = location.search.split("=")[1].split("&")[0];

        var page = location.search.split("=")[2];

        var userId = $.cookie("userId");

        function isNormalInput(ipt){
            return ipt.val() != null && ipt.val().length != 0 && ipt.val()!= '未选中';
        }

        $.post("shop/findById",{shopid:shopid},function (data) {

            data = JSON.parse(data);

            var ht = '<img id="shophs" src='+ "/FVOnline/shop/getHS?userId=" + data.userId +'>';
            $("#loadshop").html(ht);

            if (data.userId == userId){

                $("#returnshop").html('<a id="shopdetails">返回店铺</a>');
                $("#ordermanage").html('<a id="gotoordermanage">商品管理</a>');

                $("#shopdetails").click(function () {
                    location.href = "shopdetails.html?shopid=" + shopid + "&page=1";
                });

                $("#gotoordermanage").click(function () {
                    location.href = "goodsmanage.html?shopid=" + shopid + "&page=1";
                });
            }
        });


        $.post("order/findByShopId",{shopid:shopid,page:page,pageSize:10},function (data) {
            data = JSON.parse(data);

            var ht = '<div class="row list">\n' +
                '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">商品</div>\n' +
                '                    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 listtitle">收货地址</div>\n' +
                '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">订单编号</div>\n' +
                '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">用户备注</div>\n' +
                '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">快递名称</div>\n' +
                '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">快递编号</div>\n' +
                '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">操作</div>\n' +
                '                </div>';

            for (var i = 0; i < data.orders.length; i++) {



                var remarks = '';

                if (data.orders[i].remarks == null){
                    remarks = '';
                } else {
                   remarks =  data.orders[i].remarks
                }

                ht += ' <div class="row orderlist">\n' +
                    '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listorder">'+ data.orders[i].goods.goodsname +'</div>\n' +
                    '        <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 listorder" title="'+ data.orders[i].address.receiveName +'&nbsp;'+ data.orders[i].address.telephone + '&nbsp;' + data.orders[i].address.province + data.orders[i].address.city + data.orders[i].address.details +'">'+ data.orders[i].address.receiveName +'&nbsp;'+ data.orders[i].address.telephone +'&nbsp;'+ data.orders[i].address.province + data.orders[i].address.city +'...</div>\n' +
                    '        <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder">'+ data.orders[i].orderNumber +'</div>\n' +
                    '        <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder">'+ remarks +'</div>' +
                    '        <select class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listorder" id="expresstype_'+ data.orders[i].id +'">\n' +
                    '                <option value="未选中"></option>\n' +
                    '                <option value="圆通">圆通</option>\n' +
                    '                <option value="中通">中通</option>\n' +
                    '                <option value="申通">申通</option>\n' +
                    '                <option value="韵达">韵达</option>\n' +
                    '                <option value="顺丰">顺丰</option>\n' +
                    '        </select>\n' +
                    '        <input class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder" type="text" id="express_'+ data.orders[i].id +'">\n' +
                    '        <a class="col-xs-1 col-sm-1 col-md-1 col-lg-1" id="send_'+ data.orders[i].id +'">确认发货</a>\n' +
                    '   </div>';
            }

            ht += '<div id="rightbottom">共'+ Math.ceil(data.total/10) +'页,当前为第'+ page +'页<button id="prevPage">上一页</button><button id="nextPage">下一页</button>第<input type="text" id="howpage" oninput = "value = value.replace(/[^\\d]/g,\'\')">页<button id="gotoPage">跳转</button></div>';

            $("#manageright").html(ht);

            for (var i = 0; i < data.orders.length; i++) {
                $("#send_" + data.orders[i].id).click(function () {
                    var id = this.id.split("_")[1];

                    if (isNormalInput($("#expresstype_" + id)) && isNormalInput($("#express_" + id))) {
                        var expresstype = $("#expresstype_" + id).val();
                        var expressnumber = $("#express_" + id).val();

                        $.post("order/sendExpress",{id:id,expresstype:expresstype,expressnumber:expressnumber},function (data1) {
                            data1 = JSON.parse(data1);
                            if (data1.code.toLowerCase() == "success"){
                                location.href = "ordermanage.html?shopid=" + shopid + "&page=" + page;
                            } else {
                                alert("发货失败")
                            }

                        });
                    } else {
                        alert("请填写快递信息！");
                    }
                });
            }

            $("#prevPage").click(function () {
                if (page > 1){
                    location.href = "ordermanage.html?shopid=" + shopid + "&page=" + (parseInt(page)-1);
                }
            });

            $("#nextPage").click(function () {
                if (page < Math.ceil(data.total/10)){
                    location.href = "ordermanage.html?shopid=" + shopid + "&page=" + (parseInt(page)+1);
                }
            });

            $("#gotoPage").click(function () {
                var p = $("#howpage").val();
                if (0 < p && p <= Math.ceil(data.total/10)) {
                    location.href = "ordermanage.html?shopid=" + shopid + "&page=" + p;
                }
            });

        });

        $("#wait").click(function () {
            location.href = "ordermanage.html?shopid=" + shopid + "&page=1";
        });

        $("#already").click(function () {
            $("#wait").removeClass("choise");
            $("#wait").addClass("unchoise");
            $("#already").removeClass("unchoise");
            $("#already").addClass("choise");
            loadFinishOrder(shopid,1);
        });

        function loadFinishOrder(shopid,page) {

            $.post("order/findByShopIdTwo",{shopid:shopid,page:page,pageSize:10},function (data) {
                data = JSON.parse(data);

                var ht = '<div class="row list">\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">商品</div>\n' +
                    '                    <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 listtitle">收货地址</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">订单编号</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">用户备注</div>\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">快递名称</div>\n' +
                    '                    <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listtitle">快递编号</div>\n' +
                    '                    <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listtitle">操作</div>\n' +
                    '                </div>';

                for (var i = 0; i < data.orders.length; i++) {



                    var remarks = '';

                    if (data.orders[i].remarks == null){
                        remarks = '';
                    } else {
                        remarks =  data.orders[i].remarks
                    }

                    ht += ' <div class="row orderlist">\n' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listorder">'+ data.orders[i].goods.goodsname +'</div>\n' +
                        '        <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 listorder" title="'+ data.orders[i].address.receiveName +'&nbsp;'+ data.orders[i].address.telephone + '&nbsp;' + data.orders[i].address.province + data.orders[i].address.city + data.orders[i].address.details +'">'+ data.orders[i].address.receiveName +'&nbsp;'+ data.orders[i].address.telephone +'&nbsp;'+ data.orders[i].address.province + data.orders[i].address.city +'...</div>\n' +
                        '        <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder">'+ data.orders[i].orderNumber +'</div>\n' +
                        '        <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder">'+ remarks +'</div>' +
                        '        <div class="col-xs-1 col-sm-1 col-md-1 col-lg-1 listorder">'+ data.orders[i].expressName +'</div>\n' +
                        '        <div class="col-xs-2 col-sm-2 col-md-2 col-lg-2 listorder">'+ data.orders[i].expressNumber +'</div>\n' +
                        '        <p class="col-xs-1 col-sm-1 col-md-1 col-lg-1">无</p>\n' +
                        '   </div>';
                }

                ht += '<div id="rightbottom">共'+ Math.ceil(data.total/10) +'页,当前为第'+ page +'页<button id="prevPage">上一页</button><button id="nextPage">下一页</button>第<input type="text" id="howpage" oninput = "value = value.replace(/[^\\d]/g,\'\')">页<button id="gotoPage">跳转</button></div>';

                $("#manageright").html(ht);

                $("#prevPage").click(function () {
                    if (page > 1){
                        loadFinishOrder(shopid,(parseInt(page)-1));
                    }
                });

                $("#nextPage").click(function () {
                    if (page < Math.ceil(data.total/10)){
                        loadFinishOrder(shopid,(parseInt(page)+1));
                    }
                });

                $("#gotoPage").click(function () {
                    var p = $("#howpage").val();
                    if (0 < p && p <= Math.ceil(data.total/10)) {
                        loadFinishOrder(shopid,p);
                    }
                });

            });
        }

    </script>

</body>
</html>